package cn.stronglink.assetmanage.business.check.presenter;

import java.io.IOException;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.model.SyncAssetModel;
import cn.stronglink.assetmanage.business.check.model.SyncModel;
import cn.stronglink.assetmanage.business.check.view.sync.CheckSyncView;
import cn.stronglink.assetmanage.business.check.view.sync.SyncAssetView;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.index.base.BasePresenter;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by chencongcong on 2018/6/12.
 */

public class SyncAssetPresenter extends BasePresenter implements SyncAssetModel.onSyncCheckDataListener{
    private SyncAssetView syncAssetView;
    private SyncAssetModel syncAssetModel;
    //具体某个表单资源列表
    private List<Asset> orderList;

    public SyncAssetPresenter(SyncAssetView syncAssetView, SyncAssetModel syncAssetModel) {
        this.syncAssetView = syncAssetView;
        this.syncAssetModel = syncAssetModel;
        this.syncAssetModel.setSyncCheckDataListener(this);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void start() {

    }

    /**
     * 显示详情页，需要跳转到详情页界面
     *
     * @param postion
     */
    public void showDetailData(int postion) {
        //获得要显示详细信息的资产编号
        Asset asset = orderList.get(postion);
        syncAssetView.showWaitOrderDetail(asset);
    }

    public void syncCheckData() {
        syncAssetView.showProgressDialog(MyApplication.APP_CONTEXT.getString(R.string.submit_check_data));
        syncAssetModel.syncCheckData();
    }

    @Override
    public void onFailer(Call call, IOException e) {
        syncAssetView.hideProgressDialog();
        syncAssetView.showToast(e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) {
        syncAssetView.hideProgressDialog();
        if(response.isSuccessful()){
            if(response.code()==200){
                String json = response.body().toString();
                //同步数据成功后，删除本地数据
                syncAssetModel.deteleSyncData();
            }else{
                syncAssetView.showToast(response.message());
            }
        }else{
            syncAssetView.showToast(MyApplication.APP_CONTEXT.getString(R.string.sync_data_fail));
        }
    }

    /**
     *根据表单id显示表单中的资产列表
     * @param orderNo
     */
    public void showOrderDataByOrderId(String orderNo) {
        orderList = syncAssetModel.getDataByOrderNo(orderNo);
        syncAssetView.setOrderData(orderList);
        syncAssetView.showAssetTitle(syncAssetModel.getCheckByOrderNo(orderNo));
    }
}
